"use strict";
module.exports = (app) => {
  const { STRING, DATE, BOOLEAN, INTEGER } = app.Sequelize;
  const SysRole = app.model.define("sys_role", {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: "主键id，自增",
    },
    roleName: {
      type: STRING(32),
      unique: true,
      allowNull: false,
      comment: "角色名称",
    },
    roleKey: {
      type: STRING(50),
      unique: true,
      allowNull: false,
      comment: "角色标识",
    },
    status: {
      type: BOOLEAN,
      defaultValue: true,
      comment: "状态 true：有效  false：冻结",
    },
    createById: {
      type: STRING(36),
      comment: "创建人ID",
    },
    createTime: {
      type: DATE,
      comment: "创建时间",
    },
    updateById: {
      type: STRING(36),
      comment: "更新人Id",
    },
    updateTime: {
      type: DATE,
      comment: "更新时间",
    },
    delFlag: {
      type: BOOLEAN,
      defaultValue: false,
      comment: "状态 true：已删  false：未删",
    },
  });

  SysRole.associate = () => {
    // 与用户表是多对多关系
    app.model.SysRole.belongsToMany(app.model.SysUser, {
      through: app.model.SysUserRole,
      foreignKey: "roleId",
      otherKey: "userId",
    });
    // 与菜单表是多对多关系
    app.model.SysRole.belongsToMany(app.model.SysMenu, {
      through: app.model.SysRoleMenu,
      as: "menu",
      foreignKey: "roleId",
      otherKey: "menuId",
    });
  };

  return SysRole;
};
